package com.gitee.swsk33.annotationbased;

import com.gitee.swsk33.annotationbased.model.Cat;
import com.gitee.swsk33.annotationbased.service.ExampleService;
import com.gitee.swsk33.annotationbased.service.MessageService;
import com.gitee.swsk33.annotationbased.service.SendService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;

// 指定Main类为扫描起点配置类
@ComponentScan
public class Main {

	public static void main(String[] args) {
		// 以Main配置类作为起点，向下扫描相关Bean注解的类并初始化为Bean
		ApplicationContext context = new AnnotationConfigApplicationContext(Main.class);
		// 获取MessageService对象
		MessageService messageService = context.getBean(MessageService.class);
		// 然后就可以使用了！
		messageService.print();

		// 获取SendService对象
		SendService sendService = context.getBean(SendService.class);
		// 调用
		sendService.send();

		// 获取ExampleService对象
		ExampleService exampleService = context.getBean(ExampleService.class);
		// 调用
		exampleService.print();

		// 获取Bean：cat
		Cat cat = context.getBean("cat", Cat.class);
		// 打印
		System.out.println(cat);

		// 获取Bean：catTwo
		Cat catTwo = context.getBean("catTwo", Cat.class);
		System.out.println(catTwo);
	}

}